# 添加常用的公共函数
import configparser
import os
from selenium import webdriver
from Lib.common.chrome_options import Options
from Lib.common.ui_log import error_log, normal_log
import time
from selenium.webdriver.common.by import By
import yaml
from Tools.environment import Environment


# 获取项目的根路径
def get_path():
    return os.path.dirname(os.path.dirname(os.path.dirname(__file__)))


# 选择浏览器
# 当前选择的浏览器为chrome 可根据需要进行修改
def choose_browser(browser_type=''):
    '''
    根据浏览器类型打开浏览器
    :param browser_type:
    :return:
    '''
    if browser_type == 'chrome':
        normal_log().info('>>>正在加载{0}驱动'.format(browser_type))
        return webdriver.Chrome(options=Options().my_chrome_options())
    elif browser_type == 'firefox':
        normal_log().info('>>>正在加载{0}驱动'.format(browser_type))
        return webdriver.Firefox()
    elif browser_type == 'ie':
        normal_log().info('>>>正在加载{0}驱动'.format(browser_type))
        return webdriver.Ie()
    else:
        normal_log().info('>>>正在加载{0}驱动'.format('chrome'))
        return webdriver.Chrome(options=Options().my_chrome_options())


# 定制化时间格式
def get_time(format_time: str, number: int = None):
    '''
    返回特定格式的时间字符串
    :param format_time:  想要的时间格式
    :param number:  与当前时间的差值
    :return:
    '''
    try:
        if number is not None:
            time_num = time.time() + (number * 24 * 60 * 60)
            # 将时间转化为格式化时间
            return time.strftime(format_time, time.localtime(time_num))
        else:
            time_num = time.time()
            # 将时间转化为格式化时间
            return time.strftime(format_time, time.localtime(time_num))
    except Exception as e:
        error_log().debug('>>>>>时间格式错误{}'.format(format_time))
        raise e


def element_operation(obj, data):
    '''
    浏览器操作
    :param obj:
    :param data:
    :return:
    '''
    for i in range(len(data)):
        # 是否需要为元素传值
        # 调用强制等待的方法
        if data[i]['method'] == 'wait':
            getattr(obj, data[i]['method'])(data[i]['second'])
        # 刷新浏览器
        elif data[i]['method'] == 'refresh':
            getattr(obj, data[i]['method'])()
        # 调用打开链接的方法
        elif data[i]['method'] == 'open':
            if data[i]['url'] is not None:
                getattr(obj, data[i]['method'])(Environment().system_env() + data[i]['url'])
            else:
                getattr(obj, data[i]['method'])(obj.url)
        # 需要为元素传入值
        elif 'value' in data[i]:
            # 选择方法
            getattr(obj, data[i]['method'])(args=data[i]['args'],
                                            text=data[i]['value'],
                                            context=data[i]['context'])
        # elif 'time' in data[i]:
        #     # 表示要为输入框输入时间格式的值
        #     getattr(obj, data[i]['method'])(args=data[i]['args'],
        #                                     text=get_time(data[i]['time'][0], data[i]['time'][1]),
        #                                     context=data[i]['context'])
        else:
            getattr(obj, data[i]['method'])(args=data[i]['args'],
                                            context=data[i]['context'])


# 打开yaml文件，获取yaml当中的数据
def get_data_by_yaml(path):
    data = None
    with open(path, mode='rt', encoding='utf-8') as f1:
        data = yaml.load(f1, yaml.FullLoader, )
    return data


if __name__ == '__main__':
    # print(get_time('%Y-%m-%d', -20))
    print('哈' * 100)
    print(get_path())
